<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Comment_Widgets_Comment_Widget extends Gio_Core_Widget
{
	public function showAction()
	{
		$perPage 	  		= $this->getParam('limit', 10);
		$this->view->limit  = $perPage;
		
		$pageIndex 	  		= $this->getParam('pageIndex', 1);
		$offset 	  		= ($pageIndex - 1) * $perPage;
		
		$request = Gio_Core_Request::getInstance();
		$json    = new Services_JSON();
		$params  = $request->getParams();
		$paramString =  base64_encode($json->encodeUnsafe($params));
		$this->view->paramString = $paramString;
		
		/**
		 * Get comments by paramString
		 */
		$numComments = Modules_Comment_Services_Comment::countThreadComments($paramString, 'active');
		$comments 	 = Modules_Comment_Services_Comment::getThreadComments($offset, $perPage, $paramString, 'active');
		$this->view->comments = $comments;
		$this->view->numComments = $numComments;
		
		// Pager
		require_once LIB_DIR . DS . 'PEAR' . DS . 'Pager' . DS . 'Sliding.php';
		$pagerOptions = array(
			'mode'       	=> 'Sliding',
			'append'   		=> false,
    		'perPage'    	=> $perPage,
    		'delta'      	=> 3,
			'urlVar'		=> 'page',
			'path'			=> '',
			'fileName'		=> 'javascript: Comment.Widgets.Comment.loadComments(%d)',
			'separator'		=> '',
			'nextImg' 		=> '<small class="icon arrow_right"></small>',
			'prevImg'		=> '<small class="icon arrow_left"></small>',
			'altNext'		=> '',
			'altPrev'		=> '',
			'altPage'		=> '',
    		'totalItems' 	=> $numComments,
			'currentPage'	=> $pageIndex,
			'urlSeparator'	=> '/',
			'spacesBeforeSeparator' => 0,
			'spacesAfterSeparator' => 0,
			'curPageSpanPre' => '<a href="javascript: void();" class="current">',
			'curPageSpanPost' => '</a>'
		
		);
		$pager = new Pager_Sliding($pagerOptions);
		$this->view->pager = $pager;
	}
	
	public function loadAction()
	{
		$perPage 	  = (int)$this->getParam('limit', 10);
		$pageIndex 	  = (int)$this->getParam('pageIndex', 1);
		$paramString  = $this->getParam('paramString');
		$offset 	  = (int)($pageIndex - 1) * $perPage;
		/**
		 * Get comments by paramString
		 */
		$numComments = Modules_Comment_Services_Comment::countThreadComments($paramString, 'active');
		$comments 	 = Modules_Comment_Services_Comment::getThreadComments($offset, $perPage, $paramString, 'active');
		$this->view->comments = $comments;
		$this->view->numComments = $numComments;
		
		// Pager
		require_once LIB_DIR . DS . 'PEAR' . DS . 'Pager' . DS . 'Sliding.php';
		$pagerOptions = array(
			'mode'       	=> 'Sliding',
			'append'   		=> false,
    		'perPage'    	=> $perPage,
    		'delta'      	=> 3,
			'urlVar'		=> 'page',
			'path'			=> '',
			'fileName'		=> 'javascript: Comment.Widgets.Comment.loadComments(%d)',
			'separator'		=> '',
			'nextImg' 		=> '<small class="icon arrow_right"></small>',
			'prevImg'		=> '<small class="icon arrow_left"></small>',
			'altNext'		=> '',
			'altPrev'		=> '',
			'altPage'		=> '',
    		'totalItems' 	=> $numComments,
			'currentPage'	=> $pageIndex,
			'urlSeparator'	=> '/',
			'spacesBeforeSeparator' => 0,
			'spacesAfterSeparator' => 0,
			'curPageSpanPre' => '<a href="javascript: void();" class="current">',
			'curPageSpanPost' => '</a>'
		
		);
		$pager = new Pager_Sliding($pagerOptions);
		$this->view->pager = $pager;
	}
	
	public function configAction()
	{
		$this->view->moduleName = Gio_Core_Widget::getIntance()->getModuleName();
		$this->view->widgetName = Gio_Core_Widget::getIntance()->getWidgetName();
		$this->view->widgetIndex = $this->getParam('widget_index');
	}
}